<template>
  <div id="" style="height: 100%">
    <van-sticky class="search">
      <div class="searchItem">
        <van-row gutter="12">
          <van-col :span="24">
            <van-field
              v-model="searchFilter.searchString"
              placeholder="请输入阵地名称"
              right-icon="search"
              @click-right-icon="handleQuery()"
            />
          </van-col>
          <!-- <van-col :span="4" class="text-right">
          <img :src="earthImg" class="typeImg" @click="changeType()">
        </van-col> -->
        </van-row>
      </div>
      <van-tabs
        v-model="searchFilter.groundType"
        @change="handleQuery()"
        class="ground"
      >
        <van-tab v-for="item in tabList" :name="item.groundType">
          <div slot="title">
            <p class="top">{{ item.groundTypeName }}</p>
            <p class="num">{{ item.total }}</p>
          </div>
        </van-tab>
      </van-tabs>
    </van-sticky>
    <div class="groundContent">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
          :offset="10"
        >
          <van-cell
            v-for="(item, index) in dataList"
            :key="index"
            class="dateItem"
            @click.stop="handleDetail(item)"
            clickable
          >
            <div class="card">
              <div class="imgBox">
                <div
                  class="groundImg"
                  :style="{
                    background: 'url(' + setPreviewUrl(item) + ')',
                    backgroundSize: 'cover',
                    backgroundPosition: 'center',
                  }"
                ></div>
                <!-- <van-image :src="setPreviewUrl(item)" class="groundImg">
                <template v-slot:loading>
                  <van-loading type="spinner" size="20" />
                </template>
              </van-image> -->
              </div>
              <p style="color: #666666" class="textEllipsis mt4">
                名称：{{ item.groundName }}
              </p>
              <p style="color: #666666" class="textEllipsis">
                地址：{{ item.address }}
              </p>
            </div>
          </van-cell>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>
<script>
export default {
  name: "",
  props: {
    tabList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      showMap: false,
      queryParams: {
        pageNum: 0,
        pageSize: 10,
      },
      earthImg: require("../../../assets/images/earth.png"),
      dataList: [],
      loading: false,
      finished: false,
      refreshing: false,
      searchFilter: {
        groundType: "4",
      },
    };
  },
  mounted() {},
  methods: {
    handleQuery() {
      this.onRefresh();
    },
    onRefresh() {
      this.dataList = [];
      this.queryParams.pageNum = 0;
      this.finished = false;
      this.onLoad();
    },
    onLoad() {
      let me = this;
      me.loading = true;
      me.queryParams.pageNum++;
      me.getGround();
    },
    getGround() {
      let me = this;
      me.$api
        .getGroundList({
          pageParam: me.queryParams,
          groundDtoFilter: me.searchFilter,
        })
        .then((res) => {
          if (res.dataList.length == 0) {
            // 本次没有数据
            me.dataList = [];
            //me.$toast('暂无数据');
            me.refreshing = false;
            me.finished = true;
          } else {
            // 本次有数据
            me.dataList = [...me.dataList, ...res.dataList];
            me.loading = false;
            me.refreshing = false;
            me.dataList.length >= res.totalCount ? (me.finished = true) : "";
          }
        });
    },
    handleReady({ BMap, map }) {
      this.center = map.getCenter();
      this.vertexPoint = map.getBounds().getSouthWest();
    },
    setPreviewUrl(val) {
      if (val.photos && val.photos.length > 0) {
        return (
          process.env.VUE_APP_BASE_URL +
          "admin/upms/file/download?filename=" +
          val.photos[0]
        );
      } else {
        return "";
      }
    },
    handleDetail(item) {
      this.$router.push({
        path: "/middle",
        query: {
          groundId: item.groundId,
          title: item.groundName,
          type:'page'
        },
      });
    },
    changeType() {
      let me = this;
      me.showMap = !me.showMap;
      me.$emit("changeType", me.showMap);
    },
  },
};
</script>
<style lang="scss" scoped>
.mapHeight {
  width: 100%;
  height: calc(100vh - 4rem);
}

.typeImg {
  display: inline-block;
  width: 0.8rem;
  height: 0.8rem;
}
.groundContent {
  padding: 0.2rem;
  background-color: #f5f5f5;
  height: calc(100vh - 1.2rem);
  overflow-y: scroll;
}
.imgBox {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%; // 9 / 16 * 100%
  .groundImg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: inline-block;
    // width: 100%;
    // height: 3.5rem;
  }
}
</style>
